package com.jh.pattern.singleton.broke;

import com.jh.pattern.singleton.lazy.LazyInnerClassSingleton;
import com.jh.pattern.singleton.register.EnumSingleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author JH_Wong
 * @projectName newforward
 * @title SerializableSingleton.java
 * @package com.jh.pattern.brokesingleton
 * @description 序列化破坏单例
 * @date 2019-05-23 10:11
 */
public class SerializableSingleton {
    public static void main(String[] args){
        /*LazyInnerClassSingleton s1 = null;
        LazyInnerClassSingleton s2 = LazyInnerClassSingleton.getInstance();*/

        EnumSingleton s1 = null;
        EnumSingleton s2 = EnumSingleton.getInstance();

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("LazyInnerClassSingleton.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(s2);
            oos.flush();
            oos.close();

            FileInputStream fis = new FileInputStream("LazyInnerClassSingleton.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            //s1 = (LazyInnerClassSingleton) ois.readObject();
            s1 = (EnumSingleton) ois.readObject();
            ois.close();

            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s1 == s2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
